/*kvan
 *2014.5.12
 */
package com.taxsystem.likaifan.taxcompute;

import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import com.main.taxsystem.R;
import com.taxsystem.tool.HttpUtil;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SecondHandHouseCompute extends Activity {

	final int MY_NOTICE = 0x111;
	final int NAV_SETTING = 0x112;
	final int ACCOUNT_SETTING = 0x113;
	final int INDIVIDUAL_SETTING = 0x114;
	public final static String RECI_COAST = "sw.system.HTTUTIL";
	private TextView show_result;
	
	private EditText buy_price;
	private EditText sell_price;
	private EditText house_area;
	private EditText decuct_expenses;
	private EditText house_type;

	private CheckBox over_5_years;
	private CheckBox seller_only;
	private CheckBox buyer_only;
	
	private Button button;
	
	private String requestUrl;
	
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @SuppressLint("NewApi") @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second_hand_house_compute);
		ActionBar bar = getActionBar();
        bar.setHomeButtonEnabled(true);
        bar.setIcon(R.drawable.actionbar_icon);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setBackgroundDrawable(new ColorDrawable(0xff333333));       
		show_result = (TextView) findViewById(R.id.show_result);
		// 注册广播
		buy_price = (EditText) findViewById(R.id.buy_price);
		sell_price = (EditText) findViewById(R.id.sell_price);
		house_area = (EditText) findViewById(R.id.house_area);
		decuct_expenses = (EditText) findViewById(R.id.decuct_expenses);
		house_type = (EditText) findViewById(R.id.house_type);
		
		over_5_years = (CheckBox) findViewById(R.id.over_5_years);
		seller_only = (CheckBox) findViewById(R.id.seller_only);
		buyer_only = (CheckBox) findViewById(R.id.buyer_only);
		
		
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		registerReceiver(myrecive, filter);
		Thread thread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST);
				try {
					//获取服务器返回的信息
					String reslut = HttpUtil
							.getRequest(requestUrl);
					intent.putExtra("weatherinfo", reslut);
					//发送广播
					sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		};
	}

	private boolean isNoEmpty()
	{
		
		if(buy_price.length()<1||sell_price.length()<1||house_area.length()<1||decuct_expenses.length()<1||house_type.length()<1)
			return false;
		else
			return true;
			
	}
	
	public void check(View view)
	{
		if(isNoEmpty())
		{
		 requestUrl = "购入价格："+buy_price.getText()+"销售价格："+sell_price.getText()+"\n套内面积："+house_area.getText()+"扣除金额："+ decuct_expenses.getText()
	+"\n住房类型："+house_type.getText()+"是否满5年："+over_5_years.isChecked()+"\n是否买方唯一住房："+seller_only.isChecked()+"\n是否卖方唯一住房："+buyer_only.isChecked()
	+"\n查询结果:未拿到接口";
		// Toast msg = Toast.makeText(ActivityMian.this, "Message", Toast.LENGTH_LONG);
		// msg.show();
		 show_result.setText(requestUrl);
		}
		else
		{
			Toast msg = Toast.makeText(SecondHandHouseCompute.this, "请把信息填写完整再查询", Toast.LENGTH_LONG);
			msg.show();
		}
	}
	
	// 定义BroadcastReceiver 接收返回的信息
	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				//获取JSONObject对象
				JSONObject jsonobject = new JSONObject(
						intent.getStringExtra("weatherinfo"));
				JSONObject jsoncity = new JSONObject(
						jsonobject.getString("weatherinfo"));
				show_result.setText("城市:" + jsoncity.getString("city") + "\t"
						+ "日期:" + jsoncity.getString("date_y") + "\n" + "今天天气:"
						+ jsoncity.getString("temp1") + "\t"
						+ jsoncity.getString("weather1")+"\t"+jsoncity.getString("wind1"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_mian, menu);
		menu.add(0, MY_NOTICE, 0, "我的通知");
		menu.add(0, NAV_SETTING, 0, "导航设置");
		menu.add(0, ACCOUNT_SETTING, 0, "账号设置");
		menu.add(0, INDIVIDUAL_SETTING, 0, "个性化设置");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem mi)
	{
		
		
		//判断单击的是哪个菜单项，并针对性的作出响应。
		switch (mi.getItemId())
		{
			case android.R.id.home:
				this.finish();
				break;
			case MY_NOTICE:

				Toast toast1 = Toast.makeText(SecondHandHouseCompute.this,
					     "MY_NOTICE", Toast.LENGTH_SHORT);
				toast1.show();
				break;
			case NAV_SETTING:
				Toast toast2 = Toast.makeText(getApplicationContext(),
					     "NAV_SETTING", Toast.LENGTH_SHORT);
				toast2.show();;
				break;
			case ACCOUNT_SETTING:
				Toast toast3 = Toast.makeText(getApplicationContext(),
					     "ACCOUNT_SETTING", Toast.LENGTH_SHORT);
				toast3.show();
				break;
			case INDIVIDUAL_SETTING:
				Toast toast4 = Toast.makeText(getApplicationContext(),
					     "INDIVIDUAL_SETTING", Toast.LENGTH_SHORT);
				toast4.show();
				break;

		}
		return true;
	}

}
